﻿
// MFCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MFC.h"
#include "MFCDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCDlg dialog




CMFCDlg::CMFCDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CMFCDlg::IDD, pParent)
	, m_valName(_T(""))
	, m_valBirthday(_T(""))
	, m_valAddress(_T(""))
	, m_valGradSchool(_T(""))
	, m_rdSex(0)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	
}

void CMFCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_RESET, m_btnReset);
	DDX_Control(pDX, IDC_EDITNAME, m_txtName);
	DDX_Control(pDX, IDC_EDITBIRTHDAY, m_txtBirthday);
	DDX_Control(pDX, IDC_EDITADDRESS, m_txtAddress);
	DDX_Control(pDX, IDC_COMBOGRADSCHOOL, m_cbGradSchool);
	DDX_Control(pDX, IDC_LISTSTAFF, m_ctrList);
	DDX_Text(pDX, IDC_EDITNAME, m_valName);
	DDX_Text(pDX, IDC_EDITBIRTHDAY, m_valBirthday);
	DDX_Text(pDX, IDC_EDITADDRESS, m_valAddress);
	DDX_CBString(pDX, IDC_COMBOGRADSCHOOL, m_valGradSchool);
	DDX_Radio(pDX, IDC_RADIOMALE, m_rdSex);

	DDX_Control(pDX, IDC_RADIOMALE, m_ctrlMale);
	DDX_Control(pDX, IDC_RADIOFEMALE, m_rbnFemale);
}

BEGIN_MESSAGE_MAP(CMFCDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_RESET, &CMFCDlg::OnBnClickedReset)
	ON_BN_CLICKED(IDOK, &CMFCDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDC_RADIOMALE, &CMFCDlg::OnBnClickedRadiomale)
	ON_BN_CLICKED(IDC_RADIOFEMALE, &CMFCDlg::OnBnClickedRadiofemale)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LISTSTAFF, &CMFCDlg::OnLvnItemchangedListstaff)
	ON_EN_UPDATE(IDC_EDITNAME, &CMFCDlg::OnEnUpdateEditname)
END_MESSAGE_MAP()


// CMFCDlg message handlers

BOOL CMFCDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	m_ctrList.InsertColumn(0, L"Name", 0, 100);
	m_ctrList.InsertColumn(1, L"Ngày sinh", 0, 80);
	m_ctrList.InsertColumn(2, L"Giới tính", 0, 50);
	m_ctrList.InsertColumn(3, L"Địa chỉ", 0, 150);
	m_ctrList.InsertColumn(4, L"Trường tốt nghiệp", 0, 180);

	m_ctrList.ModifyStyle(0, LVS_EX_GRIDLINES, NULL);
	m_ctrList.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP | LVS_EX_ONECLICKACTIVATE);

	m_txtName.SetLimitText(40);
	m_txtBirthday.SetLimitText(10);
	m_txtAddress.SetLimitText(80);
	m_cbGradSchool.SetCurSel(0);
	// Set list colume
	// TODO: Add extra initialization here

	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMFCDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMFCDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CMFCDlg::OnBnClickedReset()
{
	// TODO: Add your control notification handler code here
	
	// Clear content of text edit "Name"
	m_txtName.SetWindowTextW(L"");
	m_txtName.Clear();

	// Clear content of text edit "Name"
	m_txtBirthday.SetWindowTextW(L"");

	// Clear content of text edit "Name"
	m_txtAddress.SetWindowTextW(L"");

	m_ctrlMale.SetCheck(TRUE);
	m_rbnFemale.SetCheck(FALSE);
	// Clear content of text edit "Name"

	m_cbGradSchool.SetWindowTextW(L"");
	m_cbGradSchool.SetCurSel(0);

	m_ctrList.DeleteAllItems();
}



void CMFCDlg::OnBnClickedOk()
{
	// TODO: Add your control notification handler code here
	//CDialogEx::OnOK();
	m_txtName.GetWindowTextW(m_valName);

	m_txtBirthday.GetWindowTextW(m_valBirthday);
	//m_txt.GetWindowTextW(m_valName);
	m_txtAddress.GetWindowTextW(m_valAddress);
	
	int currentSel = m_cbGradSchool.GetCurSel();
	if (currentSel != CB_ERR)
	{
		m_cbGradSchool.GetLBText(currentSel, m_valGradSchool);
	}
	CString s;
	//UpdateData(TRUE);
	switch(m_rdSex)
	{
	case 0:
		s = L"Nam";
		break;
	case 1:
		s = L"Nữ";
		break;
	default:
		break;
	}
	m_ctrList.InsertItem(0, m_valName);
	m_ctrList.SetItemText(0, 1, m_valBirthday);
	m_ctrList.SetItemText(0, 2, s);
	m_ctrList.SetItemText(0, 3, m_valAddress);
	m_ctrList.SetItemText(0, 4, m_valGradSchool);
}


void CMFCDlg::OnBnClickedRadiomale()
{
	// TODO: Add your control notification handler code here
	m_rdSex = 0;
	m_rbnFemale.SetCheck(FALSE);
}


void CMFCDlg::OnBnClickedRadiofemale()
{
	// TODO: Add your control notification handler code here
	m_rdSex = 1;
	m_ctrlMale.SetCheck(FALSE);
}



void CMFCDlg::OnLvnItemchangedListstaff(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
	// TODO: Add your control notification handler code here
	int curSel = 0;
	curSel = m_ctrList.GetNextItem(-1, LVIS_SELECTED);

	CString strName = m_ctrList.GetItemText(curSel, 0);
	m_txtName.SetWindowText(strName);

	CString strBirth = m_ctrList.GetItemText(curSel, 1);
	m_txtBirthday.SetWindowText(strBirth);

	CString strSex = m_ctrList.GetItemText(curSel, 2);
	if (strSex == "Nam")
	{
		m_ctrlMale.SetCheck(TRUE);
		m_rbnFemale.SetCheck(FALSE);
	} 
	else
	{
		m_ctrlMale.SetCheck(FALSE);
		m_rbnFemale.SetCheck(TRUE);
	}
	m_rdSex = 0;
	
	CString strAddress = m_ctrList.GetItemText(curSel, 3);
	m_txtAddress.SetWindowText(strAddress);
	
	CString strGradSchool = m_ctrList.GetItemText(curSel, 4);
	m_cbGradSchool.SetWindowTextW(strGradSchool);
}


void CMFCDlg::OnEnUpdateEditname()
{
	// TODO:  If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialogEx::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.

	// TODO:  Add your control notification handler code here
	/*while (IsNumber(m_valName))
	{
		MessageBox(L"Input again!", L"abc", NULL);
	}*/
	
}

//bool CMFCDlg::IsNumber( CString s )
//{
//	for (int i = 0; i < s.GetLength(); i++)
//	{
//		if (isalpha(s.GetAt(i)))
//		{
//			return false;
//		}
//	}
//	return true;
//}
